<template>
	<el-card class="box-card">
		<template #header>
			<div class="card-header">
				<span>登录</span>
			</div>
		</template>
		<div>
			<!-- 登录编码  登录密码 -->

			<el-form :model="adminForm" label-width="80px">
				<el-form-item label="登录编码">
					<el-input v-model="adminForm.adminCode" type="text" placeholder="请输入登录编码" />
				</el-form-item>
				<el-form-item label="登录密码">
					<el-input v-model="adminForm.password" type="password" show-password placeholder="请输入登录密码" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="login">登录</el-button>
				</el-form-item>
			</el-form>

		</div>
	</el-card>
</template>

<script setup>
	import {
		inject,
		reactive
	} from "vue";
	import {
		useRouter
	} from 'vue-router';

	const router = useRouter();

	const myElMessage = inject("myElMessage");

	const axios = inject("axios");


	const adminForm = reactive({
		adminCode: "admin",
		password: "123"
	});

	const login = () => {
		if (adminForm.adminCode == "") {
			myElMessage("登录编码不能为空!", "warning");
			return;
		}
		if (adminForm.password == "") {
			myElMessage("登录密码不能为空!", "warning");
			return;
		}
		axios.post("admin/SignIn", adminForm).then(function(response) {
			let r = response.data;
			if (r.ok) {
				localStorage.setItem("token", r.data.token);
				let adminId = r.data.admin.adminId;
				localStorage.setItem("adminId", adminId);
				let adminCode = r.data.admin.adminCode;
				localStorage.setItem("adminCode", adminCode);
				myElMessage("登录成功!", "message");
				router.push("/index")
			} else {
				myElMessage("登录编码或密码错误!", "error");
			}
		});
	}
</script>

<style scoped>
	.box-card {
		width: 400px;
		margin: 0 auto;
		margin-top: 150px;
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>